package com.sys.plat.web.core;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.util.MessageResources;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sys.plat.bean.PageBean;
import com.sys.plat.util.HttpUtil;


public class BaseAction extends DispatchAction {
	protected final Log log = LogFactory.getLog(getClass());
	public static final String EDIT = "edit";
	public static final String LIST = "list";
	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.warn("Did't specified the op parameter ");
		return mapping.findForward("STY_NO_OP");
	}
	
	public String go() throws Exception {
		return HttpUtil.getRequest().getParameter("go");
	}
	
	public static Object getService(String beanId){
		WebApplicationContext webApplicationContext = getWebApplicationContext();
		return webApplicationContext.getBean(beanId);
	}
	
	public static WebApplicationContext  getWebApplicationContext(){
		return WebApplicationContextUtils.getWebApplicationContext(ActionContext.getRequest().getSession().getServletContext());
	}
	/**
	 * 
	 */
	public  void addError(String key){
		ActionContext.getRequest().setAttribute("SYS_ERROR", loadMessage(key));
	}
	public  void addWarnning(String key){
		ActionContext.getRequest().setAttribute("SYS_WARN", loadMessage(key));
	}
	public  void addMsg(String key){
		ActionContext.getRequest().setAttribute("SYS_MSG", loadMessage(key));
	}
	/**
	 * 读取资源文件
	 * @param key
	 * @return
	 */
	public String loadMessage(String key){
		MessageResources messageResources=getResources(ActionContext.getRequest(),"sys_message");
		return messageResources.getMessage(key);
	}
	
	protected PageBean createPageBean(BaseForm form) {
		PageBean pageBean = new PageBean();
		pageBean.setPageno(form.getPageno());
		pageBean.setPagesize(form.getPagesize());
		pageBean.setFiltermap(form.getFilter());
		pageBean.setProperty(form.getProperty());
		pageBean.setSortOrder(form.getOrder());
		return pageBean;
	}
	
	/**
	 * 自动填充 enterpriseId,creatorId,creatorName,createDate, updateId,updateName,updateDate;
	 * @param obj
	 */
	public void updatePo(Object obj){
//		 GftUserInfo userInfo = SystemUtil.getLoginUser().getGftUserInfo();
		
//		try {
//			String id = BeanUtils.getProperty(obj, "id");
//			if(id!=null&&id.length()>0){
//				//更新
//				BeanUtils.setProperty(obj, "enterpriseId", userInfo.getEnterpriseId());
//				BeanUtils.setProperty(obj, "updateId", userInfo.getId());
//				BeanUtils.setProperty(obj, "updateName", userInfo.getUserName());
//			}else{
//				BeanUtils.setProperty(obj, "enterpriseId", userInfo.getEnterpriseId());
//				BeanUtils.setProperty(obj, "creatorId", userInfo.getId());
//				BeanUtils.setProperty(obj, "creatorName", userInfo.getUserName());
//			}
//		} catch (IllegalAccessException e) {
//			e.printStackTrace();
//		} catch (InvocationTargetException e) {
//			e.printStackTrace();
//		} catch (NoSuchMethodException e) {
//			e.printStackTrace();
//		}
	}	
}
